# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
from fake_useragent import UserAgent


"""
驱动地址：https://github.com/mozilla/geckodriver/releases
WebDriver 只能在一个页面上对元素识别与定位，对于frame/iframe 表单内嵌页面上的元素无法直接定位
需要通过switch_to.frame()方法将当前定位的主体切换为frame/iframe 表单的内嵌页面中
#切换到表单
driver.switch_to.frame("x-URS-iframe")
打开新的标签页
js = f'window.open("{url}");'
driver.execute_script(js)
-------滚动条,最下面---------
page_height = driver.execute_script('return document.documentElement.scrollHeight')
driver.execute_script(f"window.scrollBy(0, {page_height})")
===滚动到元素=====
# 滑动滚动条到某个指定的元素
div = driver.find_element_by_xpath('//div[@id="page_info"]')
js4 = "arguments[0].scrollIntoView();"
driver.execute_script(js4, div)
======ActionChains滚动======
from selenium.webdriver.common.action_chains import ActionChains
doubleclick_btn = driver.find_element_by_xpath('//div[@class="page_box"]')
ActionChains(driver).move_to_element(doubleclick_btn).perform()
------Select------
# 选中这个标签，然后使用Select创建对象
selectTag = Select(driver.find_element_by_name("jumpMenu"))
# 根据索引选择
selectTag.select_by_index(1)
# 根据值选择
selectTag.select_by_value("值")
# 根据可视的文本选择
selectTag.select_by_visible_text("显示值")
# 取消选中所有选项
selectTag.deselect_all()
------Select------
------切换窗口并关闭------
# 获取当前窗口句柄
now_handle = driver.current_window_handle
# 获取所有窗口句柄
all_handles = driver.window_handles
for handle in all_handles:
    if handle != now_handle:
        driver.switch_to.window(handle)  # 切换新打开窗口
        driver.close()
# 返回主窗口
driver.switch_to.window(now_handle)
------切换窗口并关闭------
------滚动条scrollTop=0为顶部，100000为底部------
js="var q=document.documentElement.scrollTop=100000"
------滚动条------
------返回521反爬-----
driver.get 两次.第一次获取加密cookie。第二次提交解析后的cookie
------返回521反爬-----
---------CSS 有空格------------
driver.find_element_by_css_selector('[class="btn btn-primary"]').click()
---------CSS 有空格------------
---------获取元素Selenium------
driver.find_element_by_xpath('//img[@id="ipocaptcha_CaptchaImage"]').get_attribute("src")
"""


class Firefox_Get():
    def __init__(self):
        firefox_options = Options()
        # fire_options.add_argument('--headless') # 无头模式
        firefox_options.add_argument('--disable-gpu')
        firefox_options.set_preference("general.useragent.override", 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')  # 修改请求头，伪装浏览器型号
        # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
        # 未在PATH环境变量中设置，需要设置驱动可执行路径
        self.driver = webdriver.Firefox(executable_path='D:\\webdriver\\geckodriver.exe', options=firefox_options)
        self.driver.maximize_window()  # 浏览器最大化
        # html = self.driver.page_source
        # print(html)

    # 获取页面url
    def get_contents(self, url):
        self.driver.get(url)
        # 设置等待
        wait = WebDriverWait(self.driver, 10)

        WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.XPATH, '//a[contains(text(),"下一页")]')))

        self.driver.find_element_by_css_selector('[class="searchKey search-inp"]').send_keys("鞍山市政工程有限公司")
        self.driver.find_element_by_css_selector('[class="search-rightBtn search-click"]').click()
        while True:
            html = etree.HTML(self.driver.page_source)
            for t in html.xpath('//div[@class="LM_list"]'):
                type = t.xpath('./div/div[@class="labelTwo"]/text()')[0]
                urlChild = t.xpath('./div[@class="list_title clearfix"]/h4/a/@href')[0]
                title = t.xpath('./div[@class="list_title clearfix"]/h4/a/text()')[0]
                fy = t.xpath('./div[@class="list_subtitle"]//span/text()')[0]
                haoma = t.xpath('./div[@class="list_subtitle"]//span/text()')[1]
                riqi = t.xpath('./div[@class="list_subtitle"]//span/text()')[2]
                reason = "".join(t.xpath('./div[@class="list_reason"]//text()')).strip()
                # 获取当前窗口句柄
                now_handle = self.driver.current_window_handle
                js = f'window.open("{urlChild}");'
                self.driver.execute_script(js)
                time.sleep(2)
                # 获取所有窗口句柄
                all_handles = self.driver.window_handles
                for handle in all_handles:
                    if handle != now_handle:
                        self.driver.switch_to.window(handle)
                        html = etree.HTML(self.driver.page_source)
                        aa = html.xpath('//div[@id="1"]/text()')[0]
                        reason1 = ""
                        # （正文内容）、（法官助理、书记员）
                        list1 = html.xpath('//div[contains(@style,"LINE-HEIGHT: 25pt; TEXT-INDENT: 30pt; MARGIN: 0.5pt 0cm;FONT-FAMILY: 宋体; FONT-SIZE: 15pt;")]//text()')
                        # 审判长、审判员、日期
                        list2 = html.xpath('//div[@style="TEXT-ALIGN: right; LINE-HEIGHT: 25pt; MARGIN: 0.5pt 36pt 0.5pt 0cm;FONT-FAMILY: 宋体; FONT-SIZE: 15pt;"]//text()')
                        # 标题
                        list3 = html.xpath('//div[@style="TEXT-ALIGN: center; LINE-HEIGHT: 25pt; MARGIN: 0.5pt 0cm; FONT-FAMILY: 黑体; FONT-SIZE: 18pt;"]//text()')
                        # 法官助理、书记员
                        list4 =[]
                        i = 0
                        # 添加标题
                        for t in list3:
                            reason1 += t + "\r\n"
                        # 拼接正文内容、审判长、法官助理、书记员
                        for l in list1:
                            if len(list1) - 2 == i:
                                for l1 in list2:
                                    reason1 += l1 + "\r\n"
                            reason1 += l + "\r\n"
                            if i >= len(list1) - 2:
                                list4.append(l)
                            i += 1
                        self.driver.close()
                # 返回主窗口
                self.driver.switch_to.window(now_handle)
                print("=================")
                print(type)
                print(urlChild)
                print(fy)
                print(haoma)
                print(riqi)
                print(reason)
            time.sleep(2)
            # js = "document.getElementsByClassName('pageButton')[5].click();"
            # self.driver.execute_script(js)
            # 点击下一页
            self.driver.find_element_by_xpath('//a[contains(text(),"下一页")]').click()
            # 点击下一页


if __name__ == '__main__':
    firefox_get = Firefox_Get()
    url = "https://sou.zhaopin.com/?p=2&jl=530&kw=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%B8%88&kt=3"
    firefox_get.get_contents(url)
